<?php
/**
 * 门锁/网管/房间关系管理类
 *
 * @auther 大耳牛
 * 2016-12-31
 **/
namespace Home\Model;
use Think\Model;
class LockModel extends Model{
	//定义表名称
	protected $tableName = 'lock';
	
	/**
	 * 获取网管列表总数
	 *
	 **/
	public function getLockListCount(){
		return $this->count();
	}
	
	/**
	 * 获取网管列表
	 *
	 **/
	public function getLockList($page){
		return $this->alias('slock')
					->join('left join momoda_network as network on slock.network_id = network.network_id')
					->join('left join momoda_room as room on slock.room_id = room.room_id')
					->field('slock.*, network.network_title,network.network_nums, room.room_number')
					->order('slock.lock_id asc')
					->page($page, C('PAGE_SIZE'))
					->select();
	}
	
	/**
	 * 判断是否存在关系
	 * 
	 **/
	public function isCheckLockAndNetWork($room_id, $lock_id, $network_id){
		return $this->where(array(
			'room_id' => $room_id,
			'lock_id' => $lock_id,
			'network_id' => $network_id
		))->count();
	}
	
	/**
	 * 保存信息
	 *
	 **/
	public function addLockAndNetworkAndRoom($data){
		return $this->data($data)->add();
	}
	
	/**
	 * 删除配置信息
	 *
	 **/
	public function delLockAndNetworkById($lock_id){
		return $this->where(array('lock_id' => $lock_id))->delete();
	}
	
	/**
	 * 编辑配置文件
	 **/
	public function editLockAndNetworkInfo($lock_id, $data){
		return $this->where(array('lock_id' => $lock_id))->save($data);
	}
	
	/**
	 * 根据lock日志ID查询配置详情
	 **/
	public function getLockInfoByLockId($lock_id){
		return $this->where(array('lock_id' => $lock_id))->find();
	}
	
	/**
	 * 根据房间ID判断是否存在配置关系
	 * @param int $room_id
	 **/
	public function getLockInfoByRoomNumber($room_id){
		return $this->where(array('room_id' => $room_id))->count();
	}
	
	/**
	 * 根据锁ID与网管ID判断是否存在关系
	 * @param int $cmd_id 门锁ID
	 * @param int $network_id 网管ID
	 **/
	public function getLockInfoByLockIdAndNetWorkID($cmd_id, $network_id){
		return $this->where(array('cmd_id' => $cmd_id, 'network_id' => $network_id))->find();
	}
	
	/**
	 * 根据房间号判断是否存在配置关系
	 * @param string $room_number
	 *
	 **/
	public function getLockInfoByRoomNumbers($room_number){
		return $this->alias('slock')
					->join('left join momoda_room as room on slock.room_id = room.room_id')
					->join('left join momoda_network as network on slock.network_id = network.network_id')
					->field('slock.*, network.network_nums, network.network_barcode')
					->where(array('room.room_number' => $room_number))
					->find();
	}
}